#region License Revision: 0 Last Revised: 6/18/2008 11:00 AM
/******************************************************************************
Copyright (c) Microsoft Corporation.  All rights reserved.


This file is licensed under the Microsoft Public License (Ms-PL). A copy of the Ms-PL should accompany this file. 
If it does not, you can obtain a copy from: 

http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx
******************************************************************************/
#endregion // License
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using VIEWS;

namespace SilverlightMap
{
    public partial class Page : UserControl
    {
        SLController controller;
		bool fZoomOnClick = false;

        public Page()
        {
            InitializeComponent();
            controller = new SLController(this);
            this.Loaded += new RoutedEventHandler(Page_Loaded);
       }

        void Page_Loaded(object sender, RoutedEventArgs e)
        {
            controller.Initialize("myMap");
        }

		private void CheckBox_Click(object sender, RoutedEventArgs e)
		{
			CheckBox cb = e.OriginalSource as CheckBox;
			if (cb != null)
			{
				bool check = (bool)cb.IsChecked;
				FindLayer findLayer = (FindLayer)cb.DataContext;
				findLayer.Visible = check;
			}
		}

		private void btnFind_Click(object sender, RoutedEventArgs e)
		{
			controller.Find(tbFindWhat.Text, tbFindWhere.Text);
		}

		private void btnDirections_Click(object sender, RoutedEventArgs e)
		{
			controller.GetDirections(tbFrom.Text, tbTo.Text);
			fZoomOnClick = true;
		}

		private void lbFind_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			//Could do it here, but we actually want to wait for a checkbox click: see CheckBox_Click()
		}

		private void lbRoute_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			if (e.AddedItems.Count > 0)
			{ // if an existing item is clicked on, we will show that centered on the map and zoomed in to an appropriate level
				VERouteItineraryItem routeItem = e.AddedItems[0] as VERouteItineraryItem;
				if (fZoomOnClick)
				{
					controller.Map.ZoomLevel = 15;  // we could base this zoom level on the length of the segment...
					fZoomOnClick = false;
				}
				controller.Map.Center = routeItem.LatLong;
			}
		}
    }
}
